package com.ftg.learn.demo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/6/2 下午2:24
 */
public class PubSubReceiving2 {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("kjx");
        factory.setPassword("kjx");
        factory.setVirtualHost("/kjx");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String exname = "telmess";
        //声明一下我这是一个消费者要与exchange进行通信
        channel.exchangeDeclare(exname, "fanout");

        String QUEUE_NAME = channel.queueDeclare().getQueue();
//        String QUEUE_NAME = "kjxqueue";
        channel.queueBind(QUEUE_NAME,exname,"");

        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        //监听队列里面是否有数据

        //有就是getmessage 没有waiting
        DeliverCallback deliverCallback = (x, y) -> {
            String mess = new String(y.getBody(), "utf-8");

            //这段代码是否可以自己处理一下  service 添加数据库

            //可以收接到是短信 {telphone:13******,mess:'fdsafdsfdsfsdafdsfs'}
            //调用sms 直接发信息了

            System.out.println(mess + "||=>这是我的消费信息");
        };
        //消费者端
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
        });
    }

}
